﻿@model IEnumerable<CoreLims.Models.CoreLims.SampleAnalyte>

@{
    ViewBag.Title = "分析项管理";
}

<div class="row col-md-12 ml-0" style="min-height:5vh;">
    <div class="col-3 pr-0" style="display: none">
        <select id="Analyte-EqTypes"></select>
    </div>
    <strong class="font-rfs" style="margin-right: 2rem;padding-top: 0.4rem;">分析项管理</strong>
    <select id="Select-Classification" class="form-control font-rfs custom-headtoolbar-select">
        @foreach (var item in Model)
        {
            <option>@item.Classification</option>
        }
    </select>
    <button id="addAnalyte" class="btn btn-rfs"><i class="bi-plus-square"></i> 添加</button>
    <button id="editAnalyte" class="btn btn-rfs"><i class="bi-pencil-square"></i> 编辑</button>
    <button id="deleteAnalyte" class="btn btn-rfs"><i class="bi-trash"></i> 删除</button>
</div>
<div class="row col-md-12 ml-0" style="min-height:90vh;">
    <div class="row col-md-12" style="padding-right:0;">
        <div class="table-responsive-sm shadow p-0 bg-white rounded col-md-12 pre-scrollable" style="min-height:90vh;">
            <table id="Analyte" class="table table-striped table-bordered table-sm table-hover">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>代码</th>
                        <th>英文名</th>
                        <th>中文名</th>
                        <th>分类</th>
                        <th>描述</th>
                        <th>结果模式</th>
                        <th>修约</th>
                        <th>单位(原)</th>
                        <th>单位(终)</th>
                        <th>默认指标名</th>
                        <th>检出限</th>
                        <th>默认值</th>
                        <th>备选结果</th>
                        <th>方法</th>
                        <th>附件</th>
                        <th>报告过滤</th>
                        <th>CMA</th>
                        <th>CNAS</th>
                        <th>设备类型</th>
                    </tr>
                </thead>
                <tbody></tbody>
            </table>
        </div>
    </div>
</div>
<div class="modal" id="SAMPLE-AddAnalyte-myModal" role="dialog" aria-hidden="true" data-backdrop="static">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class='modal-header'>
                <p id="modalTitle" class="pt-3 pl-3 font-rfs"></p>
                <a data-dismiss="modal" class="modal-close">&times;</a>
                <label id="SAMPLE-AddAnalyte-ModalID" style="display:none">SAMPLE-AddAnalyte-myModal</label>
            </div>
            <div class="modal-body">
                <div id="Embed"></div>
            </div>
        </div>
    </div>
</div>
<div class="modal" id="DeleteConfirm" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class='modal-header'>
                <p class="pt-3 pl-3 font-rfs">删除</p>
                <a data-dismiss="modal" class="modal-close">&times;</a>
            </div>
            <div class="modal-body">
                <p class="font-rfs">确认删除当前选中记录吗？</p>
            </div>
            <div class="modal-Delete-footer">
                <button id="modal-delete" class="btn btn-outline-primary border-radius-noradius noLeftBorder noRightBorder font-rfs" style="width:50%; height:fit-content">删除</button>
                <button id="modal-cancel" data-dismiss="modal" class="btn btn-outline-primary border-radius-noradius noRightBorder font-rfs" style="width:50%; height:fit-content">取消</button>
            </div>
        </div>
    </div>
</div>

@section Scripts{
    <script>
        var analyteId = "";
        var types = [];

        $(document).ready(function () {
            $("#Select-Classification").prepend(new Option("", ""));
            $("#Select-Classification").val("");
            LoadEquipmentTypeList('Analyte-EqTypes');
        });

        $("#Select-Classification").change(function () {
            var columns = [{ field: "id", visible: false }, { field: "code" }, { field: "english" }, { field: "name" }, { field: "classification" },
            { field: "description" }, { field: "resultModel" }, { field: "roundingOff" }, { field: "units" }, { field: "fUnits" }, { field: "defaultSpecName" },
            { field: "detectionLimit", width: "5%" }, { field: "defaultValue", width: "5%" }, { field: "alternativeResults", width: "5%" },
            { field: "methodCode", width: "5%" }, { field: "attachment", width: "5%", formatter: setButton },
            { field: "assist", width: "5%", align: "center", formatter: SetAssist }, { field: "cma", width: "5%", align: "center", formatter: SetCMA },
            { field: "cnas", width: "5%", align: "center", formatter: SetCNAS }, { field: "eqType", formatter: SetTypes }];

            analyteId = "";  /*清空该值*/
            $('#Analyte').bootstrapTable('destroy'); // 这行代码必须加，否则数据无法刷新
            $('#Analyte').bootstrapTable({
                method: 'post',
                url: "GetAnalyteListByClassification",
                dataType: "json",  // 服务器端返回值类型
                contentType: "application/x-www-form-urlencoded; charset=UTF-8", // 如果是 POST 请求，这个属性必须加
                queryParams: function (params) {
                    var data = {
                        Classification: $("#Select-Classification").find("option:selected").text(),  // 传递的参数，这的名称要和控制器里对应的参数名称一样
                    };
                    return data;
                }, //参数
                queryParamsType: "search", //参数格式,发送标准的RESTFul类型的参数请求

                uniqueId: "id",          // 设置主键
                //search: true,  // 显示查询工具栏
                //showRefresh: true,  // 显示刷新按钮
                //showColumns: true,  // 可控制列的隐藏和显示
                pagination: true,  // 显示分页条,设为true，启用了分页功能
                pageSize: 15,   // 默认页大小
                pageList: [5, 10, 15, 20],  // 控制每页数目
                //showToggle: true,  //
                locale: "zh-CN",
                columns: columns,          // 加载列定义, 注意！这个地方绑定的列名称开头大写字母会全部变成小写。
                onClickRow: function (row, $element, field) {
                    $("#Analyte").find("tbody").find("tr").each(function () {
                        $(this).removeClass('changeblueColor');
                    });
                    $element.addClass("changeblueColor");
                    analyteId = row.id;
                },
                silent: true,  //刷新事件必须设置
                formatLoadingMessage: function () {
                    return "请稍等，正在加载中...";
                },
                formatNoMatches: function () {  //没有匹配的结果
                    return '无符合条件的记录';
                },
                onLoadError: function (data) {
                    $('#Analyte').bootstrapTable('removeAll');
                },
                onLoadSuccess: function (data) {   // 数据加载成功后触发该事件
                    SetBootstrapStyle();
                },
                formatShowingRows: function (pageFrom, pageTo, totalRows) {
                    return "正在显示第" + pageFrom + "到第" + pageTo + "条记录，总共" + totalRows + "条记录 ";
                },
                formatRecordsPerPage: function (pageNumber) {
                    return " 每页显示" + pageNumber + '条记录';
                }
            });
        });

        function SetTypes(value, row, index) {
            var strHtml = "<select class='table-cell-select' onchange='UpdateEqType(this)'>";
            var array = $('#Analyte-EqTypes option').toArray();
            $(array).each(function () {
                if (value == $(this).html()) {
                    strHtml = strHtml + "<option selected='true'>" + $(this).html() + "</option>";
                } else {
                    strHtml = strHtml + "<option>" + $(this).html() + "</option>";
                }
            })
            return strHtml + "</select>";
        }

        function UpdateEqType(obj) {
            $.post("UpdateAnalyteEqtype", {
                Id: $(obj).parent().parent().attr('data-uniqueid'),
                EqType: $(obj).val()
            }, function (data) {
                if (data.indexOf("成功") <= 0) {
                    alert(data);
                }
            });
        }

        function setButton(value, row, index) {
            var strHtml = "<button class='table-cell-button btn-rfs' data-analyteId=" + row.id + " onclick='TableCellButton(this)'><i class='bi-tools'></i></button>";
            return strHtml;
        }

        function TableCellButton(obj) {
            $("#modalTitle").text("分析项附件管理");

            $("#SAMPLE-AddAnalyte-myModal").modal({
                backdrop: 'static',
                keyboard: false
            });

            $.post("/Common/ManageFile", {
                Domain: "Analyte",
                DomainId: $(obj.outerHTML).attr("data-analyteId"),
                ModalID: "SAMPLE-AddAnalyte-myModal"
            }, function (datas) {
                $("#Embed").html(datas);
            });
        }

        function SetAssist(value, row, index) {
            var strHtml = "";
            if (row.assist) {
                strHtml = "<input type='checkbox' checked onclick='AssistOnClick(this)' data-rowid='" + row.id + "'/>";
            } else {
                strHtml = "<input type='checkbox' onclick='AssistOnClick(this)' data-rowid='" + row.id + "'/>";
            }
            return strHtml;
        }

        function SetCMA(value, row, index) {
            var strHtml = "";
            if (row.cma) {
                strHtml = "<input type='checkbox' checked onclick='CMAOnClick(this)'/>";
            } else {
                strHtml = "<input type='checkbox' onclick='CMAOnClick(this)'/>";
            }
            return strHtml;
        }

        function SetCNAS(value, row, index) {
            var strHtml = "";
            if (row.cnas) {
                strHtml = "<input type='checkbox' checked onclick='CNASOnClick(this)'/>";
            } else {
                strHtml = "<input type='checkbox' onclick='CNASOnClick(this)'/>";
            }
            return strHtml;
        }

        function AssistOnClick(obj) {
            var st = $(obj).prop("checked");
            var rowid = $(obj).attr("data-rowid");

            $.post("UpdateAssist", {
                Id: rowid,
                Assist: st
            }, function (data) {
                if (data.indexOf("成功") < 0) {
                    alert(data);
                }
            });
        }

        function CMAOnClick(obj) {
            var st = $(obj).prop("checked");
            var rowid = $(obj).parent().parent().attr('data-uniqueid');

            $.post("UpdateCMA", {
                Id: rowid,
                CMA: st
            }, function (data) {
                if (data.indexOf("成功") < 0) {
                    alert(data);
                }
            });
        }

        function CNASOnClick(obj) {
            var st = $(obj).prop("checked");
            var rowid = $(obj).parent().parent().attr('data-uniqueid');

            $.post("UpdateCNAS", {
                Id: rowid,
                CNAS: st
            }, function (data) {
                if (data.indexOf("成功") < 0) {
                    alert(data);
                }
            });
        }

        $("#deleteAnalyte").click(function () {
            if (analyteId == "") {
                return false;
            }

            $("#DeleteConfirm").modal({
                backdrop: 'static',
                keyboard: false
            });
        });

        $("#modal-delete").click(function () {
            $.post("postDeleteAnalyte", {
                Id: analyteId
            }, function (data) {
                if (data == "删除成功！") {
                    $('#Analyte').bootstrapTable('removeByUniqueId', analyteId);  // 删除指定的行
                    analyteId = "";  // 删除行后，将这个值置空，否则直接编辑信息就会报错。
                } else {
                    alert(data);
                }
            });

            $("#DeleteConfirm").modal('hide');
        });

        $("#addAnalyte").click(function () {
            $("#modalTitle").text("添加分析项");

            $("#SAMPLE-AddAnalyte-myModal").modal({
                backdrop: 'static',
                keyboard: false
            });

            $("#addAnalyte", parent.document.body).attr("src", "AddAnalyte");
            $.post("AddAnalyte", {
                ModalID: "SAMPLE-AddAnalyte-myModal"
            }, function (datas) {
                $("#Embed").html(datas);
            });
        });

        $("#editAnalyte").click(function () {
            if (analyteId.length == 0) {
                alert("请先选择需要编辑的分析项！");
                return;
            }

            $("#modalTitle").text("编辑分析项");

            $("#SAMPLE-AddAnalyte-myModal").modal({
                backdrop: 'static',
                keyboard: false
            });

            $("#editAnalyte", parent.document.body).attr("src", "AddAnalyte");
            $.post("AddAnalyte", {
                id: analyteId,
                ModalID: "SAMPLE-AddAnalyte-myModal"
            }, function (datas) {
                $("#Embed").html(datas);
            });
        });

        $('#SAMPLE-AddAnalyte-myModal').on('hidden.bs.modal', function (event) {
            if (event.target == this) {
                if (analyteId.length > 0) {
                    $.post("GetAnalyte", {
                        Id: analyteId
                    }, function (data) {
                        var row = $('#Analyte').bootstrapTable('getRowByUniqueId', analyteId);
                        row.code = data.code;
                        row.english = data.english;
                        row.name = data.name;
                        row.classification = data.classification;
                        row.description = data.description;
                        row.resultModel = data.resultModel;
                        row.roundingOff = data.roundingOff;
                        row.units = data.units;
                        row.fUnits = data.fUnits;
                        row.defaultSpecName = data.defaultSpecName;
                        row.detectionLimit = data.detectionLimit;
                        row.defaultValue = data.defaultValue;
                        row.alternativeResults = data.alternativeResults;
                        row.methodCode = data.methodCode;
                        row.attachment = data.attachment;
                        row.assist = data.assist;
                        row.releaseStatus = data.releaseStatus;
                        $('#Analyte').bootstrapTable('updateByUniqueId', row);
                    });
                }
            }
        });

    </script>
}
